package com.liruo.learn.spring.mvc.controller.valid;

import com.liruo.learn.spring.mvc.validate.TestLog;
import lombok.Data;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;

/**
 * @Author:liruo
 * @Date:2023-06-10-23:06:49
 * @Desc
 */
@Validated
@RequestMapping("/normal")
@RestController
public class NormalController {
    @Data
    public static class Bean {
        @TestLog("校验name")
        private String name;

        @TestLog(value = "校验name2-Create", groups = Groups.Create.class)
        private String name2;

        @TestLog(value = "校验name3-Update", groups = Groups.Update.class)
        private String name3;
    }


    /**
     * 重复校验反例
     */
    @PostMapping("/Valid/{id}")
    public String post(@Valid @NotNull @TestLog("io1") @RequestBody Bean bean, @Min(2) @PathVariable("id") Integer id) {
        //annotationValue=校验name, value=123
        //TestIO annotationValue=io1, value=NormalController.Bean(name=123)
        //annotationValue=校验name, value=123
        return "id=" + id + " " + bean.toString();
    }

    @PostMapping("/Validated/{id}")
    public String get(@Validated @NotNull @TestLog("io1") @RequestBody Bean bean, @Min(2) @PathVariable("id") Integer id) {
        //annotationValue=校验name, value=awd
        //TestIO annotationValue=io1, value=NormalController.Bean(name=awd)
        return "id=" + id + " " + bean.toString();
    }


    interface Groups{
        interface Create {}
        interface Update {}
    }
    @PostMapping("/Validated/group/Update/{id}")
    public String postUpdateGroup(@Validated({Default.class, Groups.Update.class}) @NotNull @TestLog("io1") @RequestBody Bean bean, @Min(2) @PathVariable("id") Integer id) {
        //annotationValue=校验name, value=1
        //annotationValue=校验name3-Update, value=1
        //TestIO annotationValue=io1, value=NormalController.Bean(name=1, name2=1, name3=1)
        return "id=" + id + " " + bean.toString();
    }
    @PostMapping("/Validated/group/Create/{id}")
    public String postCreateGroup(@Validated({Default.class, Groups.Create.class}) @NotNull @TestLog("io1") @RequestBody Bean bean, @Min(2) @PathVariable("id") Integer id) {
        //annotationValue=校验name, value=w
        //annotationValue=校验name2-Create, value=w
        //TestIO annotationValue=io1, value=NormalController.Bean(name=w, name2=w, name3=w)
        return "id=" + id + " " + bean.toString();
    }

}
